function saveconfig( filePath, hObject )
%SAVECONFIG Save the user specified configuration to config file. 
%   Save the user specified configuraion to config file.
%
%   INPUT ARGUMENTS
%   filePath: path to the config.mat file, either an absolute path or
%   partial path is OK.
%   hObject: a handle of one object on the config GUI, which is used to
%   provide all the handles of the GUI with the help of guihandles;
%
%   saveconfig(filePath, hObject);

%   Copyright 2012-2015 Kang M.X. Beijing Normal University.
%   $Revision: 1.0.0 $ $Date: 2014/4/25 16:30:00 $

%% Load the old version of config file
load(filePath, '-mat');

%% Get the handles of the config GUI
handles = guidata(hObject);

%% Get the configurations from the config GUI
% Save the Amplifier settings
switch get(handles.popSamplingRate, 'Value')
    case 1
        SamplingRate = 200;
    case 2
        SamplingRate = 250;
    case 3
        SamplingRate = 300;
    case 4
        SamplingRate = 500;
    case 5
        SamplingRate = 5000;
    otherwise
        error('UTILITIES:LOADCONFIG:INVALIDSCONFIGURATION', ...
            'No such sampling rate available!');
end
Config.Amplifier.SamplingRate = SamplingRate;
Config.Amplifier.RealtimeWindowLength = str2double(get(handles.txtRealtimeWindowLength, 'String'));

% Save the Channel Selection settings
Channels = get(handles.tblChannelSelection, 'Data');
switch get(handles.popBrainAmpNo, 'Value')
    case 1
        Config.Amplifier.BrainAmpNo = 1;
        Config.Channels(1:32, :) = Channels;        
    case 2
        Config.Amplifier.BrainAmpNo = 2;
        Config.Channels = Channels;
    otherwise
end


% Save the SyncBox settings
EnableSyncBox = get(handles.chkEnableSyncBox, 'Value') == get(handles.chkEnableSyncBox, 'Max');
if EnableSyncBox 
    Config.SyncBox.Enable = true;
else
    Config.SyncBox.Enable = false;
end
Config.SyncBox.ScannerClockFrequency = str2double(get(handles.txtScannerClockFrequency, 'String'));

DisableSyncMarker = get(handles.chkDisableSyncMarker, 'Value') == get(handles.chkDisableSyncMarker, 'Max');
if DisableSyncMarker
    Config.SyncBox.DisableSyncMarker = true;
else
    Config.SyncBox.DisableSyncMarker = false;
end
Config.SyncBox.SyncMarkerUpdateInterval = str2double(get(handles.txtSyncMarkerUpdateInterval, 'String'));

% Save the Storage settings
Config.Storage.StoragePath = get(handles.txtStoragePath, 'String');

% Save the Filter settings
HighCutoffStrings = get(handles.popHighCutoff, 'String');
strCurSelection = HighCutoffStrings{get(handles.popHighCutoff, 'Value')};
if strcmp(strCurSelection, 'off')
    HighCutoff = 0;     % 0 for off
else
    HighCutoff = str2double(strCurSelection);
end
Config.Filter.HighCutoff = HighCutoff;

LowCutoffStrings = get(handles.popLowCutoff, 'String');
strCurSelection = LowCutoffStrings{get(handles.popLowCutoff, 'Value')};
if strcmp(strCurSelection, 'off')
    LowCutoff = 0;      % 0 for off
else
    LowCutoff = str2double(strCurSelection);  
end
Config.Filter.LowCutoff = LowCutoff;

EnableNotchFilter = get(handles.chkNotchFilter, 'Value') == get(handles.chkNotchFilter, 'Max');
if EnableNotchFilter
    Config.Filter.NotchFilter.Enable = true;
    if get(handles.popNotchFilter, 'Value') == 1
        Config.Filter.NotchFilter.Frequency = 50;
    elseif get(handles.popNotchFilter, 'Value') ==2
        Config.Filter.NotchFilter.Frequency = 60;
    end
else
    Config.Filter.NotchFilter.Enable = false;
end


%% Update the configuration to the config file
save(filePath, 'Config', '-v7.3');

end

